package cn.stylefeng.guns.modular.cb.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbIndustryPosition;
import cn.stylefeng.guns.modular.cb.mapper.CbIndustryPositionMapper;
import cn.stylefeng.guns.modular.cb.model.params.CbIndustryPositionParam;
import cn.stylefeng.guns.modular.cb.model.result.CbIndustryPositionResult;
import  cn.stylefeng.guns.modular.cb.service.CbIndustryPositionService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wj
 * @since 2021-01-16
 */
@Service
public class CbIndustryPositionServiceImpl extends ServiceImpl<CbIndustryPositionMapper, CbIndustryPosition> implements CbIndustryPositionService {

    @Override
    public void add(CbIndustryPositionParam param){
        CbIndustryPosition entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CbIndustryPositionParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CbIndustryPositionParam param){
        CbIndustryPosition oldEntity = getOldEntity(param);
        CbIndustryPosition newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CbIndustryPositionResult findBySpec(CbIndustryPositionParam param){
        return null;
    }

    @Override
    public List<CbIndustryPositionResult> findListBySpec(CbIndustryPositionParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CbIndustryPositionParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CbIndustryPositionParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CbIndustryPosition getOldEntity(CbIndustryPositionParam param) {
        return this.getById(getKey(param));
    }

    private CbIndustryPosition getEntity(CbIndustryPositionParam param) {
        CbIndustryPosition entity = new CbIndustryPosition();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
